package org.bpms.controller.admin;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.bpms.interceptor.WebInterceptor;
import org.bpms.model.admin.Sdept;
import org.bpms.model.admin.Suser;
import org.bpms.plugins.bjui.BjuiRender;
import org.bpms.service.admin.DeptService;
import org.bpms.service.admin.LogService;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import com.jfinal.aop.Clear;
import com.jfinal.aop.Enhancer;
import com.jfinal.core.Controller;
import com.jfinal.plugin.redis.Redis;

/**
 * 后台部门控制器
 * @author hupei
 *
 */
@Clear(WebInterceptor.class)
public class DeptController extends Controller {
	private DeptService deptService = Enhancer.enhance(DeptService.class);
	private LogService  logService = Enhancer.enhance(LogService.class);
	/**
	 * 全部部门
	 */
	public void index(){
		List<Sdept> ls = new ArrayList<Sdept>();
		Sdept dept = new Sdept();
		dept.set("id", null);
		dept.set("pid", null);
		deptService.allDept(ls, dept);
		setAttr("sdepts", ls);
		render("/admin/dept/dept.html");
	}
	
	/**
	 * 保存部门树
	 */
	public void saveTree(){
		String depts = getPara("depts");
		ObjectMapper mapper = new ObjectMapper();
		Map<String, Object> jsonmap = new HashMap<String , Object>();
		mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		Map map = null;
		try {
			map = mapper.readValue(depts, Map.class);
			if (null != map){
				deptService.saveTree(map);
				String sessionId = getCookie("adminsession");
				Suser user = Redis.use().get(sessionId);
				logService.create(user.getStr("username"), "调整组织结构");
			}
		} catch (JsonParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			jsonmap.put("success", false);
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			jsonmap.put("success", false);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			jsonmap.put("success", false);
		}
		jsonmap.put("success", true);
		renderJson(jsonmap);		
	}
	
	/**
	 * 单个保存
	 */
	public void save(){
		Map<String, Object> map = new HashMap<String , Object>();
		Sdept dept = getModel(Sdept.class);
		String leader = getPara("leader" , "");
		dept.set("leader", leader);
		deptService.saveOrUpdate(dept);
		String sessionId = getCookie("adminsession");
		Suser user = Redis.use().get(sessionId);
		logService.create(user.getStr("username"), "新增部门:"+dept.getStr("text"));
		map.put("success", true);
		map.put("obj", dept);
		renderJson(map);
	}
	
	/**
	 * 保存部门用户关系
	 */
	public void deptUserSave(){
		String users = getPara("users");
		String deptId = getPara("deptid");
		ObjectMapper mapper = new ObjectMapper();
		Map<String, Object> jsonmap = new HashMap<String , Object>();	
		mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		Map map = null;
		try {
			map = mapper.readValue(users, Map.class);
			if (null != map){
				deptService.updateUserRelation(map, deptId);
			}
		} catch (IOException e) {
			render(BjuiRender.error("保存失败！" , false));
			e.printStackTrace();
		}
		render(BjuiRender.success("保存成功！" , false));		
	}
	
	/**
	 * 删除部门
	 */
	public void remove(){
		Map<String, Object> map = new HashMap<String , Object>();
		String deptId = getPara("id");
		Sdept dept = Sdept.me.findById(deptId);
		String sessionId = getCookie("adminsession");
		Suser user = Redis.use().get(sessionId);
		logService.create(user.getStr("username"), "删除部门:"+dept.getStr("text"));
		deptService.delDept(deptId);
		map.put("success", true);
		renderJson(map);
	}
}
